<template>
    <div class="resetpwd-page">
      <el-card>
        <template #header>
          <span style="font-size: 16px; color: #131523; font-weight: 500;">重置密码</span>
        </template>
        <el-form label-width="110px" :model="form" :rules="rules" ref="form">
          <el-form-item label="原密码" prop="old_pwd">
            <el-input style="width: 400px;" v-model="form.old_pwd" placeholder="请输入原密码"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input style="width: 400px;" v-model="form.new_pwd" placeholder="请输入新密码"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="re_pwd">
            <el-input style="width: 400px;" v-model="form.re_pwd" placeholder="请再次输入新密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="background-color: #4F81FF; color: rgba(255,255,255,0.9);" @click="submit">修改密码</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>

<script>
import { updateUserPwd } from '@/api/user'
export default {
  name: 'ResetPwd',
  data () {
    const checkRePassword = (rule, value, callback) => {
      if (value !== this.form.new_pwd) {
        callback(new Error('两次输入密码不一致！'))
      }
      callback()
    }
    return {
      form: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      rules: {
        old_pwd: [
          { required: true, message: '请输入原密码！', trigger: ['blur', 'change'] },
          { min: 6, max: 15, message: '密码长度需要6~15个字符', trigger: ['blur', 'change'] }
        ],
        new_pwd: [
          { required: true, message: '请输入新密码！', trigger: ['blur', 'change'] },
          { min: 6, max: 15, message: '密码长度需要6~15个字符', trigger: ['blur', 'change'] }
        ],
        re_pwd: [
          { required: true, message: '请输入再次输入新密码！', trigger: ['blur', 'change'] },
          { min: 6, max: 15, message: '密码长度需要6~15个字符', trigger: ['blur', 'change'] },
          { validator: checkRePassword, trigger: 'blur' }
        ]
      }
    }
  },
  async created () {
    // const res = await updateUserPwd(this.form)
    // console.log(res)
  },
  methods: {
    async submit () {
      // 校验输入是否合规
      await this.$refs.form.validate()
      //   调用修改密码接口
      const res = await updateUserPwd(this.form)
      //   判断是否为更新成功提示
      if (res.message !== '更新密码成功！') {
        this.$message.error(res.message)
        return
      }

      //   提示用户修改密码成功
      this.$message.success(res.message)

      // 清空输入内容并跳转至登录页
      this.reset()
      this.$router.push('/login')
    },
    reset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style>

</style>
